package cn.yusei.teamTribe.service.team;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.yusei.teamTribe.constant.PermissionType;
import cn.yusei.teamTribe.constant.ResponseCode;
import cn.yusei.teamTribe.constant.RoleType;
import cn.yusei.teamTribe.dao.team.TeamMemberDAO;
import cn.yusei.teamTribe.dao.team.TeamMemberRequestDAO;
import cn.yusei.teamTribe.dao.team.TeamRoleDAO;
import cn.yusei.teamTribe.dao.team.po.TeamMemberPO;
import cn.yusei.teamTribe.dao.team.po.TeamMemberRequestPO;
import cn.yusei.teamTribe.dao.team.po.TeamRolePO;
import cn.yusei.teamTribe.entity.TeamMemberRequest;
import cn.yusei.teamTribe.exception.TeamMemberRequestNotExistsException;
import cn.yusei.teamTribe.service.PermissionService;

@Service
public class TeamMemberService {

	@Autowired
	private TeamMemberDAO tmDAO;
	@Autowired
	private TeamMemberRequestDAO tmrDAO;
	@Autowired
	private TeamRoleDAO trDAO;
	@Autowired
	private TeamRoleService trService;
	@Autowired
	private PermissionService permissionService;
	
	/**
	 * 添加 team 成员
	 * 不进行权限判断
	 * @param userId
	 * @param teamId
	 * @return
	 */
	public int add(int userId, int teamId) {
		TeamMemberPO tmPO = new TeamMemberPO();
		tmPO.setUserId(userId);
		tmPO.setTeamId(teamId);
		if(tmDAO.exists(tmPO)) {
			return ResponseCode.ALREADY_IN_TEAM;
		}
		tmDAO.insert(tmPO);
		return ResponseCode.REQUEST_SUCCESSED;
	}
	
	/**
	 * 添加加入 team 的请求
	 * @return
	 */
	public int addRequest(int userId, int teamId, String reason) {
		TeamMemberPO tmPO = new TeamMemberPO();
		tmPO.setUserId(userId);
		tmPO.setTeamId(teamId);
		if(tmDAO.exists(tmPO)) {
			return ResponseCode.ALREADY_IN_TEAM;
		}
		TeamMemberRequestPO tmrPO= new TeamMemberRequestPO();
		tmrPO.setUserId(userId);
		tmrPO.setTeamId(teamId);
		tmrPO.setRequestReason(reason);
		tmrDAO.insert(tmrPO);
		return ResponseCode.REQUEST_SUCCESSED;
	}
	
	public TeamMemberRequest getRequest(int id) {
		TeamMemberRequestPO tmrPO = tmrDAO.select(id);
		if(tmrPO == null) {
			throw new TeamMemberRequestNotExistsException("加入社团申请id [" + id + "] 不存在");
		}
		TeamMemberRequest tmr = new TeamMemberRequest();
		BeanUtils.copyProperties(tmrPO, tmr);
		return tmr;
	}
	
	/**
	 * 
	 * @param userId
	 * @param requestId 要回应的 request 的 id
	 * @param result
	 * @param reason
	 * @return
	 */
	public int setResponse(int userId, int requestId, int result, String reason) {
		Integer teamId = tmrDAO.selectTeamId(requestId);
		if(teamId == null) {
			return ResponseCode.MEMBER_REQUEST_NOT_EXISTS;
		}
		if(!permissionService.havePermission(userId, teamId, PermissionType.TEAM_MEMBER_MANAGE, 0)) {
			return ResponseCode.PERMISSION_FORBIDDEN;
		}
		TeamMemberRequestPO tmrPO = tmrDAO.select(requestId);
		if(tmrPO.getResult() != 0) {
			return ResponseCode.MEMBER_REQUEST_ALREADY_DEALED;
		}
		tmrPO.setId(requestId);
		tmrPO.setResult(result);
		tmrPO.setResponseReason(reason);
		tmrDAO.update(tmrPO);
		return ResponseCode.REQUEST_SUCCESSED;
	}
	
	/**
	 * 删除 team 成员
	 * 只有管理员能删除别人，管理员不能被（包括会长）删除
	 * @param userId
	 * @param teamId
	 * @return
	 */
	public int delete(int userId,int targetId, int teamId) {
		//不是自己退出
		if(userId != targetId) {
			if(!permissionService.havePermission(userId, teamId, PermissionType.TEAM_MEMBER_MANAGE, 0)
					|| permissionService.havePermission(targetId, teamId, PermissionType.TEAM_MEMBER_MANAGE, 0)) {
				return ResponseCode.PERMISSION_FORBIDDEN;
			}
		}
		//是会长不能退出
		if(trService.get(teamId, RoleType.TEAM_MASTER)[0] == targetId) {
				return ResponseCode.MASTER_CAN_NOT_EXIT_TEAM;
		}
		TeamMemberPO tmPO = new TeamMemberPO();
		tmPO.setUserId(targetId);
		tmPO.setTeamId(teamId);
		if(!tmDAO.exists(tmPO)) {
			return ResponseCode.NOT_IN_TEAM;
		}
		//删除管理员身份
		TeamRolePO teamRolePO = new TeamRolePO();
		teamRolePO.setUserId(targetId);
		teamRolePO.setTeamId(teamId);
		teamRolePO.setRoleId(RoleType.TEAM_MANAGER);
		trDAO.deleteRole(teamRolePO);
		tmDAO.delete(tmPO);
		return ResponseCode.REQUEST_SUCCESSED;
	}
	
	/**
	 * 
	 * @return 发出请求的用户的 id
	 */
	public int getRequestUserId(int id) {
		return tmrDAO.selectUserId(id);
	}
	
	public int[] getMembers(int teamId, int offset, int num) {
		int[] ids = null;
		ids = tmDAO.selectByTeam(teamId, offset, num);
		return ids;
	}
	
	public int selectLastInsertId() {
		return tmrDAO.selectLastInsertId();
	}
	
	public boolean isMember(int userId, int teamId) {
		TeamMemberPO tmPO = new TeamMemberPO();
		tmPO.setUserId(userId);
		tmPO.setTeamId(teamId);
		return tmDAO.exists(tmPO);
	}
}
